package com.nuc.thread.cas.automicrefrence;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description 原子更新器
 * @createTime 2021年07月12日 17:48:00
 */
@Slf4j
public class Test3 {
    public static void main(String[] args) {

        Student student = new Student();
        AtomicReferenceFieldUpdater updater =
                AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");

        System.out.println(updater.get(student));
        System.out.println(updater.compareAndSet(student, null, "张三"));
        System.out.println(updater.get(student));
    }

}

class Student{
    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}